package com.guomao.controller;

import com.guomao.entity.Order;

import com.guomao.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Controller
public class MyControllerG {


    /*@RequestMapping("/login")
    public String login(String username, String password) {
        System.out.println(username + "," + password);
        return "success";
    }*/

  /*  @RequestMapping("/login")
    public String login() {

        return "login";
    }*/


    @RequestMapping("/handle01")
    public String handle01(@Valid User user, BindingResult result, Model m) {

//        if (user.getEmail().length())
        System.out.println(user);
        System.out.println(result.hasFieldErrors());
        System.out.println(result.hasErrors());

        HashMap<Object, Object> map = new HashMap<>();

        if (result.hasErrors()) {
            List<FieldError> list = result.getFieldErrors();
            for (FieldError fieldError : list) {
                System.out.println(fieldError.getField());
                System.out.println(fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            m.addAttribute("errors", map);
            System.out.println("数据有误");

            return "forward:/index.jsp";
        } else {
            System.out.println("数据正常");
        }
        return "success";
    }

/*    @RequestMapping("/handle01")
    public String handle01(@Valid User user, BindingResult result, Model m) {
//        System.out.println(user);
//        System.out.println(result.hasFieldErrors());
//        System.out.println(result.hasErrors());

        System.out.println(user);

        HashMap<Object, Object> map = new HashMap<>();

        //hasErrors() 有属性校验失败
        if (result.hasErrors()) {
            //getFieldErrors获取属性错误星系
            List<FieldError> list = result.getFieldErrors();
            for (FieldError fieldError : list) {
                System.out.println(">>>>>>>>>>>>>>" + fieldError.getField());//获取校验失败的属性名
//                System.out.println(fieldError.getDefaultMessage());//获取校验失败的提示信息
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            m.addAttribute("errors", map);
            System.out.println("数据有误");

            return "login";
        } else {
            System.out.println("数据正常");
        }
        return "success";
    }*/
}
